import { createRouter, createWebHistory } from 'vue-router'
import cookie from 'js-cookie'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect:"/index/dashboard",
    },
    {
      path:"/index",
      name:"Index",
      component: () => import('../views/Index.vue'),
      children:[
        {
          name:'redirect',
          path:'redirect/:backpath(.*)',
          component:()=> import('@/views/redirect/Index.vue'),
          meta:{
            title:"home"
          }
        },
        {
          name:'userinfo',
          path:'userinfo',
          component:()=> import('@/views/admin/Userinfo.vue'),
          meta:{
            title:"userinfo"
          }
        },
        {
          name:'updateadmin',
          path:'updateadmin',
          component:()=> import('@/views//admin/Updatepassword.vue'),
          meta:{
            title:"updateadmin"
          }
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue')
    },
    {
      path: '/404',
      component: () => import('../views/404.vue')
    },
    {
      path:'/:pathMatch(.*)+',
      name: 'NotFound',
      redirect:"/404"
    }
  ]
})
// 路由守卫
router.beforeEach((to,form)=>{
const token=cookie.get('token')//获得令牌
if (!token&&to.name!='login') {//没有令牌且不是登录页面就跳转登录页面
  router.replace({path:'/login',query:{back:to.fullPath}})//将当前路由路径添加到登录头，为了知道从哪个页面退出来的
  return false//取消导航
}
return true//验证通过跳转路由
})

export default router
